<?php

namespace App\Models\User;

use App\Models\BaseModel;

class Address extends BaseModel
{

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'user_id',
        'province_id',
        'city_id',
        'district_id',
        'address',
        'default',
    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'created_at',
        'updated_at',
    ];

    protected $table = 'user_address';
    protected $dateFormat = 'U';


    /**
     * 获取用户全部地址
     * @param $user_id
     * @return mixed
     */
    public function getAllAddress($user_id){
        $data = $this->where('user_id', $user_id)
            ->orderBy('default', 'desc')
            ->get();
        return $this->after($data);
    }

    /**
     * 设置默认地址
     * @param $user_id
     * @param $address_id
     */
    public function setDefault($user_id, $address_id){
        $this->where('user_id', $user_id)
            ->update(['default'=>0]);
        $this->where('user_id', $user_id)
            ->where('id', $address_id)
            ->update(['default'=>1]);
    }
}
